/**  
 *  GenevaJS, version x
 *  (c) 2009 Rick waldron
 *
 *  GenevaJS is freely distributable under the terms of an MIT license.
 *
 *--------------------------------------------------------------------------


*/
String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g,"");
};

// $() revision to support $$() behavior
// this is really ugly. 
function $(element) {
  
  var tmp  = ret  = [];
  var args = arguments;
  
  if ( args.length > 1 ) {
    for (var i = 0, length = args.length; i < length; i++)    
      tmp.push( $(args[i]) );
    
    tmp.flatten().each( function (t) {
      ret.push(t);
    })
    return ret;
  }
  
  if ( Object.isString(element) ) {
    if ( element.indexOf('.') === 0 ) {
      $$( element ).each( function ( arg ) {
        ret.push(  Element.extend( arg ) );  
      });
      return ret;
    }

    var e_id  = element;
    element   = document.getElementById(  element );
    
    $$(element.tagName).each( function (arg) {
      if ( arg.hasAttribute('id') && arg.id === e_id ) {
        ret.push(  Element.extend( arg ) );  
      }
    });
    return ret;
  }
  return Element.extend(element);
}


/**--------------------------------------------------*/

var Geneva      = {};

Geneva.Methods  = {};

$w("blur focus load resize scroll unload click dblclick " +
  "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
  "change select submit keydown keypress keyup error").each( 
   function ( type ) {
  
   Geneva.Methods[ type ]  = function ( element, fn ) {
   
    if (  typeof arguments[0] === "function" || 
            Object.isArray(this) ) { 
            
      fn  = arguments[0];
      this.invoke("observe", type, function( evt ) {
        if ( evt.target === this ) 
          fn.call( evt.target, evt);
      }); //fn = arguments[0];

      return this;
    } else {
      element = $(element)
      
      document.observe( type, function( evt ) {
        if ( evt.target === $( element ) )
          fn.call( evt.target, evt );
      });
      return element;
    }     

    return element;
  };
});


[ Event, Event.Methods, Element, Element.Methods, Form.Element.Methods, Array.prototype ]
  .each( function (destination) {
  Object.extend( destination, Geneva.Methods );
});




Object.extend(document, (function () {
  
  var meths = {};
  Object.keys( Geneva.Methods ).each(function ( method ) {
    meths[ method ] = Element.Methods[ method ].methodize();
  });

  return meths;
})());












  
Geneva.Methods['val'] = function ( element, v ) {
  
  element = $(element);

  if ( v === undefined )
    return ( element.value || element.innerHTML ).trim(); 
  else {
    if ( typeof v === "function" )
      return element.update( v() );

    if ( typeof v === "object" ) {
      element.update();
      v.toArray().each( function ( k ) {
        element.insert( typeof k === "function" ? k() : k );
      });          
    }
    else
      return element.update( v );
  }
  return element;
};


Element.addMethods( Geneva.Methods );
Element.addMethods();
